//
//  HOPatrolViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 6/15/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOPatrolViewController.h"
#import "HOPatrolListViewController.h"
#import "HOPotalLocationViewController.h"
#import "HOPotalPointEntity.h"
#import "HOConstants.h"
#import "HOUtils.h"
#import "NDate.h"

@interface HOPatrolViewController ()<HOPotalLocationViewControllerDelegate>
{
}
@property(nonatomic, retain)HOPotalPointEntity *pointEntity;
@property(nonatomic, retain)NSString *time;
@property(nonatomic, retain)NSString *date;
@property(nonatomic, retain)NSArray *potalTimes;
@end


@implementation HOPatrolViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[self initTableView];
	[self initPickerView];
	[self initMediaView];
	[self registerKeyboard];
	[self registerTapGesture];
	[self initSubviews];
	self.takePhotoMgr.onlyCanTakePhoto = YES;
}

- (void)initSubviews
{
	baseTableview.tableFooterView = [self getFooterViewWithTitle:@"提交"];
	self.potalTimes = @[@"08:00", @"14:00", @"20:00"];
	self.time = [self.potalTimes firstObject];
	self.date = [HOUtils getCurrentDate];
	pickerViewSelectRow = 0;
}

- (void)hideKeyboard
{
	[[self.reportDescEditView getTextView] resignFirstResponder];
}

- (void)viewWillAppear:(BOOL)animated
{
	self.tabBarController.tabBar.hidden = YES;
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:HOLocalizedString(@"巡查", @"巡查")];
	[self displayRightButtonItemWithTitle:NSLocalizedString(@"巡查列表", @"巡查列表")];
	[self.reportDescEditView setPlaceHolder:NSLocalizedString(@"请输入巡查记录", @"请输入巡查记录")];

}
- (void)rightButtonItemDidClick
{
	HOPatrolListViewController *viewCtrl = [[HOPatrolListViewController alloc] init];
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

#pragma mark
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
	[self hideKeyboard];
}

- (void)onKeyboardWillShow:(NSNotification *)notification
{
	[super onKeyboardWillShow:notification];
	[baseTableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	return 3;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	return [self.potalTimes objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	pickerViewSelectRow = row;
}

- (void)didPickDone:(id)sender
{
	[super didPickDone:sender];
	self.time = [self.potalTimes objectAtIndex:pickerViewSelectRow];
	[baseTableview reloadData];
}

- (void)didSelectPotalPoint:(HOPotalPointEntity *)entity
{
	self.pointEntity = entity;
	[baseTableview reloadData];
}

- (void)dealloc
{
	self.pointEntity = nil;
	self.time = nil;
	self.date = nil;
	self.potalTimes = nil;
	[super dealloc];
}

- (void)didClickFooterButton:(UIButton *)btn
{
	
	if(self.pointEntity == nil)
	{
		[self popupHintView:NSLocalizedString(@"请选择巡查点！", @"请选择巡查点！")];
		return;
	}
	NSString *content = [self getTrimingCharaFromTextView:[self.reportDescEditView getTextView]];
	if(content == nil || content.length == 0)
	{
		[self popupHintView:NSLocalizedString(@"请输入巡查记录！", @"请输入巡查记录！")];
		return;
	}
	
	NSString *timeStr = [NSString stringWithFormat:@"%@ %@", self.date, self.time];
	double time = [HOUtils getTimeStampByTimeStr:timeStr];	
	NSMutableDictionary *dic = [NSMutableDictionary
								dictionaryWithDictionary:@{kHO_FIELD_Action:kHO_ACTION_SubmitInspect,
														   kHO_FIELD_InspectLocaitonId:[NSNumber numberWithInt:self.pointEntity.pointId],
														   kHO_FIELD_Content:content,
														   kHO_FIELD_InspectTime:[NSNumber numberWithInteger:pickerViewSelectRow]}];
	NSString *images = [self getSelectedImages];
	if(images != nil && images.length > 0)
	{
		[dic setObject:images forKey:kHO_FIELD_Inspectimage];
	}
	[self sendRequestWithParams:dic];
}

#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.section == 2)
		return 280;
    return CELL_HEIGHT;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	if(section == 0)
		return 2;
	else if(section == 1)
		return 1;
	else if(section == 2)
		return 1;
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
	if(section == 2)
	{
		CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
		UIView *view = [[[UIView alloc]
				   initWithFrame:CGRectMake(0,
											0,
											tableView.frame.size.width,
											height)] autorelease];
		[view.layer setMasksToBounds:YES];
		
		UILabel *lbl = [[UILabel alloc]
						initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
												 (44 - 22) / 2,
												 tableView.frame.size.width - 2 * HO_HORIZONAL_PADDING,
												 22)];
		lbl.text = NSLocalizedString(@"巡查记录", @"巡查记录");
		lbl.textColor = TEXT_COLOR_666666;
		lbl.font = [UIFont systemFontOfSize:15];
		[view addSubview:lbl];
		[lbl release];
		return view;
	}
	return nil;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	if(section == 2)
		return CELL_HEIGHT;
    return SECTION_PADDING / 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];

	if(indexPath.section == 0
	   || indexPath.section == 1)
	{
		static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
		UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
		if(cell == nil)
		{
			cell = [[[UITableViewCell alloc]
					 initWithStyle:UITableViewCellStyleDefault
					 reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
		}
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		UILabel *lbl = (UILabel *)[cell viewWithTag:115];
		if(lbl == nil)
		{
			lbl = [[UILabel alloc]
				   initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
											(cellHeight - 22) / 2,
											tableView.frame.size.width - 2 * HO_HORIZONAL_PADDING,
											22)];
			lbl.tag = 115;
			[cell.contentView addSubview:lbl];
			lbl.textColor = TEXT_COLOR_181818;
			lbl.font = [UIFont systemFontOfSize:15];
			[lbl release];
		}
		lbl = (UILabel *)[cell viewWithTag:115];
		lbl.hidden = YES;
		if(indexPath.section == 0 && indexPath.row == 0)
		{
			lbl.hidden = NO;
			lbl.text = [NSString stringWithFormat:@"日期：%@", self.date];
		}
		else if(indexPath.section == 0 &&  indexPath.row == 1)
		{
			lbl.hidden = NO;
			lbl.text = [NSString stringWithFormat:@"时间：%@", self.time];
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			cell.selectionStyle = UITableViewCellSelectionStyleDefault;
		}
		else if(indexPath.section == 1 && indexPath.row == 0)
		{
			lbl.hidden = NO;
			if(self.pointEntity)
			{
				lbl.text = [NSString stringWithFormat:@"巡查点：%@", self.pointEntity.pointName];
			}
			else
			{
				lbl.text = NSLocalizedString(@"请选择巡查点", @"请选择巡查点");
			}
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			cell.selectionStyle = UITableViewCellSelectionStyleDefault;
		}
		return cell;
	}
	else if(indexPath.section == 2)
	{
		return [self tableView:tableView cellForPatrolRowAtIndexPath:indexPath];
	}
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	if(indexPath.section == 0 && indexPath.row == 1)
	{
		[self showPickerView:YES];
	}
	if(indexPath.section == 1 && indexPath.row == 0)
	{
		HOPotalLocationViewController *viewCtrl = [[HOPotalLocationViewController alloc] init];
		viewCtrl.delegate = self;
		[self.navigationController pushViewController:viewCtrl animated:YES];
		[viewCtrl release];
	}
	
}
#pragma mark

#pragma mark -- tool
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_SubmitInspect])
	{
		self.view.userInteractionEnabled = NO;
		[self popupHintView:@"提交成功!" inTime:1.5 needCallback:YES];
	}
}

- (void)hintDidHide
{
	self.view.userInteractionEnabled = YES;
	[self goBack];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
